let listLoad = require('../../common/mixinList');
Page({
    behaviors: [listLoad],
    data: {
        reqUrl: '/supplier/account/index',
        login: true,
        choose: false, //是否是选择
    },
    onLoad(options) {
        const choose = options.choose === 'true'
        if (choose) {
            wx.showToast({
                title: '请点击要选择的银行卡',
                icon: 'none',
                duration: 2000
            })
            this.setData({ choose })
        }
    },
    onShow: function() {
        this.getList(true)
    },
    operation({ mark }) {
        const { index, setDefault, del } = mark
        if (index == undefined) return
        if (setDefault) return this.setDefault(index)
        if (del) return this.del(index)
        if (this.data.choose) {
            const eventChannel = this.getOpenerEventChannel()
            wx.navigateBack({
                success: () => {
                    eventChannel.emit('setBankInfo', { data: this.data.list[index] })
                }
            })
        }
    },
    //设置默认
    setDefault(index) {
        if (this.data.list[index].is_default == 1) return
        wx.$dialog.confirm({
            title: '提示',
            message: '您确定设置为默认银行卡吗？',
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    return this.request({
                        url: '/supplier/account/default',
                        method: 'POST',
                        data: {
                            account_id: this.data.list[index].account_id
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '设置成功',
                            icon: 'success'
                        })
                        const previousIndex = this.data.list.findIndex(item => {
                            return item.is_default == 1
                        })

                        this.setData({
                            ['list[' + index + '].is_default']: 1
                        })
                        if (previousIndex !== -1) this.setData({
                            ['list[' + previousIndex + '].is_default']: 0
                        })
                    }, () => {
                        resolve()
                    })
                }
                resolve(true);
            })
        })
    },
    //删除
    del(index) {
        wx.$dialog.confirm({
            title: '提示',
            message: '您确定删除此银行卡吗？',
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    this.request({
                        url: '/supplier/account/del',
                        method: 'POST',
                        data: {
                            account_id: this.data.list[index].account_id
                        }
                    }, () => {
                        resolve(true)
                        const { list } = this.data
                        list.splice(index, 1)
                        this.setData({ list })
                        wx.showToast({
                            title: '删除成功',
                            icon: 'success'
                        })
                    }, () => {
                        resolve()
                    })
                    return
                }
                resolve(true);
            })
        })
    }
})